<template>
  <el-card class="login-card">
    <h2 class="login-title">Login</h2>
    <el-form class="login-form">
      <el-form-item label="Username">
        <el-input v-model="username"></el-input>
      </el-form-item>
      <el-form-item label="Password">
        <el-input v-model="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login" style="width: 100%;">Login</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import { ref } from 'vue';
import { ElCard, ElForm, ElFormItem, ElInput, ElButton, ElMessage } from 'element-plus';
import {useRouter} from 'vue-router';
import axios from 'axios';

const router = useRouter();

const username = ref('admin');
const password = ref('admin');

const login = async() => {
  // 这里可以添加登录逻辑
  console.log('Username:', username.value);
  console.log('Password:', password.value);
  await axios.get("api/login", {
    params: {
      username: username.value,
      password: password.value
    }
  }).then(res => {
    console.log('res:===>',res.data);
    if (res.data != '')  {
      ElMessage.success('Login success!');
      localStorage.setItem('access-token',JSON.stringify(res.data))
      console.log('token:===>',JSON.parse(localStorage.getItem('access-token')))
      router.push('/home');
    } else {
      ElMessage.error('Login failed!');
    }
  }).catch(err => {
    console.log(err);
  });
};
</script>

<style scoped>
.login-card {
  width: 400px;
  margin: 50px auto;
}

.login-form {
  width: 80%;
  margin: 0 auto;
}
.login-title {
  text-align: center;
  margin-bottom: 20px;
}
</style>
